package 力扣_滑动窗口.第三类模板;

/**
 * @author zx
 * @create 2022-05-10 9:25
 */
public class 子数组最大平均数I_643 {
    /**
     * @return 滑动窗口(官方)
     */
    public double findMaxAverage(int[] nums, int k) {
        int windowSize = k;
        double sum = 0;
        for(int i = 0;i < windowSize;i++){
            sum += nums[i];
        }
        double maxSum = sum;
        int left = 0;
        for(int right = windowSize;right < nums.length;right++){
            sum += nums[right];
            left = right - windowSize;//窗口左值是固定的：right - windowSize
            sum -= nums[left];
            maxSum = Math.max(sum,maxSum);
        }
        return maxSum / k;
    }
}
